package com.zhiwang.planet.ui.my.record

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.zhiwang.planet.R
import com.zhiwang.planet.databinding.BeanDetailListLayoutBinding
import com.zhiwang.planet.di.component.DaggerAccountComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvvm.viewmodel.my.EnergyDetailViewModel
import com.zhiwang.planet.ui.BaseFragment
import com.zhiwang.planet.util.SimpleDiffCallBack
import kotlinx.android.synthetic.main.bean_detail_list_layout.*
import javax.inject.Inject


class EnergyFragment : BaseFragment() {


    lateinit var adapter: CommonRecordAdapter


    @Inject
    lateinit var diamondDetailViewModel: EnergyDetailViewModel

    var page = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        DaggerAccountComponent.builder()
                .accountModule(AccountModule(context as AppCompatActivity))
                .appComponent(getAppComponent())
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        val view = inflater.inflate(R.layout.bean_detail_list_layout, container, false)
        val binding = DataBindingUtil.bind<BeanDetailListLayoutBinding>(view)
        binding?.viewModel = diamondDetailViewModel
        binding?.lifecycleOwner = viewLifecycleOwner
        return view
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        adapter = CommonRecordAdapter(SimpleDiffCallBack())
        recyclerView.adapter = adapter
        adapter.setTypeIconResource(R.drawable.ic_detail_bean)
        smartRefresh.setEnableAutoLoadMore(true)
        emptyHintTextView.text = "您还暂未有过趣豆收入哦~"
        diamondDetailViewModel.datas.observe(viewLifecycleOwner, Observer {
            adapter.submitList(it)
        })
//        diamondDetailViewModel.refreshState.observe(viewLifecycleOwner, Observer(diamondDetailViewModel.refreshing::set))
//        diamondDetailViewModel.loadMoreState.observe(viewLifecycleOwner, Observer(diamondDetailViewModel.loadingMore::set))
        diamondDetailViewModel.err.observe(viewLifecycleOwner, Observer(this::onError))

    }


}
